import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    cart:[],
  },
  mutations: {
    addcart(state,obj){
      state.cart.push(obj)
    },
    //数量加
    addnum(state,index){
      state.cart[index].number++
    },
    // 数量减
    addcut(state,index){
      state.cart[index].number--
    },
  },
  actions: {
  },
  modules: {
  },
  getters:{
    count(state){
      var count=0;
      state.cart.forEach((item,key)=>{
        count += item.price*item.number
      })
      return count
    },
    num(state){
      var num=0;
      state.cart.forEach((item,key)=>{
        num += item.number
      })
      return num
    }
  }
})
